/*
 * Copyright 2017-2101 Innel.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hykj.xxltest.controller;

import com.github.pagehelper.PageInfo;
import com.hykj.xxltest.base.AppBack;
import com.hykj.xxltest.base.RequiresRole;
import com.hykj.xxltest.model.UserMessageModel;
import com.hykj.xxltest.model.UserModel;
import com.hykj.xxltest.service.UserMessageService;
import com.hykj.xxltest.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 私信
 *
 * @author innel
 * @date 2018-04-07 13:25:14
 */
@Controller
@RequestMapping("/usermessage")
@Api(description = "私信")
public class UserMessageController {

    private static final Logger logger = Logger.getLogger(UserMessageController.class);

    @Autowired
    private UserMessageService userMessageService;

    @Autowired
    private UserService userService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @ResponseBody
    @RequiresRole(role = "0,1")
    @ApiOperation(
            value = "获取私信列表",
            notes = "备注：",
            httpMethod = "POST")
    public AppBack list(@ApiParam(value = "每页显示个数,不填默认10")
                        @RequestParam(required = false)
                                Integer pageSize,
                        @ApiParam(value = "用户ID")
                        @RequestParam(required = false)
                                Integer userId,
                        @ApiParam(value = "是否已读 1已读 0未读")
                        @RequestParam(required = false)
                                Integer hasread,
                        @ApiParam(value = "token")
                        @RequestParam(required = false)
                                String token,
                        @ApiParam(value = "开始时间 范例 2017-11-06 16:02:11", hidden = true)
                        @RequestParam(required = false)
                                String startTime,
                        @ApiParam(value = "截止时间 格式 YYYYMMDD 也可以", hidden = true)
                        @RequestParam(required = false)
                                String endTime,
                        @ApiParam(value = "搜索关键字")
                        @RequestParam(required = false)
                                String searchKey,
                        @ApiParam(value = "是否是后台列表请求", hidden = true)
                        @RequestParam(required = false)
                                String backList,
                        @ApiParam(value = "是否简单显示", hidden = true)
                        @RequestParam(required = false)
                                String noD,
                        @ApiParam(value = "不填不分页")
                        @RequestParam(required = false)
                                Integer page) {
        Map<String, Object> params = new HashMap<>();
        params.put("pageSize", pageSize);
        params.put("userId", userId);
        params.put("hasread", hasread);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        params.put("searchKey", searchKey);
        params.put("noD", noD);
        params.put("backList", backList);
        if (page == null) {
            return new AppBack(userMessageService.getDList(params));
        }
        PageInfo<UserMessageModel> p = userMessageService.getDList(params, page);
        return new AppBack(p);
    }


    /**
     * 信息
     */
    @RequestMapping("/info")
    @ResponseBody
    @RequiresRole(role = "0,1")
    @ApiOperation(
            value = "获取私信 同时设置已读",
            notes = "备注：",
            httpMethod = "POST")
    public AppBack info(@ApiParam(value = "用户ID")
                        @RequestParam(required = false)
                                Integer userId,
                        @ApiParam(value = "token")
                        @RequestParam(required = false)
                                String token,

                        @ApiParam(value = "私信的ID", required = true)
                        @RequestParam Integer userMessageId) {
        UserMessageModel userMessage = userMessageService.getByIdD(userMessageId);
        if (userMessage == null) {
            return new AppBack(-1, "当前资源不存在");
        }
        userMessage.setHasread(1);
        userMessageService.update(userMessage);
        return new AppBack(userMessage);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @ResponseBody
    @RequiresRole(role = "1")
    @ApiOperation(
            value = "保存私信",
            notes = "备注：",
            httpMethod = "POST")
    public AppBack save(@ApiParam(value = "token")
                        @RequestParam(required = false)
                                String token,
                        @ApiParam(value = "内容", required = true)
                        @RequestParam
                                String content,
                        @ApiParam(value = "用户ID", required = true)
                        @RequestParam
                                Integer userId,
                        @ApiParam(value = "是否已读 1已读 0未读", required = true)
                        @RequestParam
                                Integer hasread,
                        @ApiParam(value = "标题", required = true)
                        @RequestParam
                                String title,
                        @ApiParam(value = "选填参数控制 默认不填", hidden = true)
                        @RequestParam(required = false)
                                Integer extra) {
        UserMessageModel userMessage = new UserMessageModel();
        userMessage.setContent(content);
        userMessage.setUserId(userId);
        userMessage.setHasread(hasread);
        userMessage.setTitle(title);
        userMessageService.create(userMessage);
        return new AppBack();

    }


    /**
     * 保存
     */
    @RequestMapping("/send")
    @ResponseBody
    @RequiresRole(role = "1")
    @ApiOperation(
            value = "群发",
            notes = "备注：",
            httpMethod = "POST")
    public AppBack send(
                        @ApiParam(value = "内容", required = true)
                        @RequestParam
                                String content,
                        @ApiParam(value = "用户ID", required = true)
                        @RequestParam
                                Integer userId,
                        @ApiParam(value = "标题", required = true)
                        @RequestParam
                                String title) {
        if(userId!=null){
            UserMessageModel userMessage = new UserMessageModel();
            userMessage.setContent(content);
            userMessage.setUserId(userId);
            userMessage.setHasread(0);
            userMessage.setTitle(title);
            userMessageService.create(userMessage);
            return new AppBack();
        }

        List<UserModel> userList = userService.getListLite(new HashMap());
        for(UserModel userModel:userList){
            UserMessageModel userMessage = new UserMessageModel();
            userMessage.setContent(content);
            userMessage.setUserId(userModel.getId());
            userMessage.setHasread(0);
            userMessage.setTitle(title);
            userMessageService.create(userMessage);
        }

        return new AppBack();

    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @ResponseBody
    @RequiresRole(role = "1")
    @ApiOperation(
            value = "更新私信",
            notes = "备注：",
            httpMethod = "POST")
    public AppBack update(@ApiParam(value = "token")
                          @RequestParam(required = false)
                                  String token,
                          @ApiParam(value = "内容")
                          @RequestParam(required = false)
                                  String content,
                          @ApiParam(value = "用户ID")
                          @RequestParam(required = false)
                                  Integer userId,
                          @ApiParam(value = "是否已读 1已读 0未读")
                          @RequestParam(required = false)
                                  Integer hasread,
                          @ApiParam(value = "标题")
                          @RequestParam(required = false)
                                  String title,
                          @ApiParam(value = "私信的ID", required = true)
                          @RequestParam int userMessageId) {
        UserMessageModel userMessage = new UserMessageModel();
        userMessage.setContent(content);
        userMessage.setUserId(userId);
        userMessage.setHasread(hasread);
        userMessage.setTitle(title);
        userMessage.setId(userMessageId);

        userMessageService.update(userMessage);
        return new AppBack();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ResponseBody
    @RequiresRole(role = "1")
    @ApiOperation(
            value = "删除当前资源",
            notes = "备注：",
            httpMethod = "POST")
    public AppBack delete(@ApiParam(value = "用户ID")
                          @RequestParam(required = false)
                                  Integer userId,
                          @ApiParam(value = "token")
                          @RequestParam(required = false)
                                  String token,
                          @ApiParam(value = "私信的ID", required = true)
                          @RequestParam
                                  int userMessageId) {

        userMessageService.deleteById(userMessageId);
        return new AppBack();
    }

}
